package cn.changeforyou.analyzer.javaparserDemo;

import cn.changeforyou.utils.io.read.ReadUtils;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ParserConfiguration.LanguageLevel;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;

import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.NormalAnnotationExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.google.common.io.Files;
import java.io.*;
import java.util.List;
import java.util.Optional;
import lombok.Data;
import org.checkerframework.checker.units.qual.A;
import org.junit.jupiter.api.Test;

/**
 * 官网demo
 */
public class JavaparserOfficalNetStationDemo extends BaseTest{
	
	@Test
	public  void main() throws IOException {
		String path = "D:\\code\\analyzer\\analyzer\\src\\main\\resources\\srcFiles\\HelloWorld.java";
		CompilationUnit compilationUnit = getCompilationUnit(path);
		ClassOrInterfaceDeclaration helloWorld = compilationUnit.getClassByName("HelloWorld").get();
		
		AnnotationExpr a_Exp = helloWorld.getAnnotationByClass(A.class).get();
		NormalAnnotationExpr normalAnnotationExpr = new NormalAnnotationExpr();
		normalAnnotationExpr.setName("Data");
		helloWorld.tryAddImportToParentCompilationUnit(Data.class);
//		NodeList<MemberValuePair> pairs = new NodeList<>();
//		MemberValuePair memberValuePair = new MemberValuePair();
//		memberValuePair.setName("length").setValue()
//		pairs.add();
//		normalAnnotationExpr.setPairs(pairs).setName("A");
		helloWorld.addAnnotation(normalAnnotationExpr);
		class MyVisitor<Void> extends VoidVisitorAdapter<Void> {
			@Override
			public void visit(final NormalAnnotationExpr n, final Void arg) {
				System.out.println(n.getPairs().get(0).getValue());
				n.getPairs().forEach(p -> p.accept(this, arg));
				n.getName().accept(this, arg);
				n.getComment().ifPresent(l -> l.accept(this, arg));
			}
		}
		a_Exp.accept(new MyVisitor(), null);

//		compilationUnit.findAll(FieldDeclaration.class).stream().filter(f -> f.isPublic() && f.isStatic())
//			.forEach(f -> System.out.println("Check field at line " + f.getRange().map(r -> r.begin.line).orElse(-1) + "  fieldName =" + f.getElementType()));
		FieldDeclaration field = helloWorld.getFieldByName("field").get();
//		System.out.println(field);
		List<MethodDeclaration> li = compilationUnit.findAll(MethodDeclaration.class);
		li.stream().forEach(f -> System.out.println(f.getName()));
		Optional<MethodDeclaration> sayHello_optional = li.stream().filter(f -> f.getName().getIdentifier().equals("sayHello")).findAny();

		if(sayHello_optional.isPresent()) {
			System.out.println("find method sayHello");
			MethodDeclaration methodDeclaration = sayHello_optional.get();
//			methodDeclaration.setName("sayHello");
//			methodDeclaration.setLineComment("lineComment");
//			methodDeclaration.setBlockComment("blockComment");
			methodDeclaration.setJavadocComment("java docComment");
		}
		System.out.println(helloWorld.getParentNode().get().toString());
		Files.write(compilationUnit.toString().getBytes("UTF-8"), new File(path));
	}
}
